# -*- coding: utf-8 -*-
from config import * 
from tingshu import *
import json
import urllib2
import httplib  
import socket 
import time
import datetime
import os
from dehtml import * 
from pyquery import PyQuery as pq
import chardet
import sys
from lxml import etree
reload(sys)
sys.setdefaultencoding( "utf-8" )
httplib.HTTPConnection._http_vsn = 10  
httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'  
socket.setdefaulttimeout(10)

def download_source(img_url, path,img_name):   
    try:  
        request = urllib2.Request(img_url)  
        request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5')  
        img_stream = urllib2.urlopen(request)  
        img_path = path +'/'+img_name
        img_file = open(img_path, 'wb')  
        img_file.write(img_stream.read())  
        img_file.close() 
        return img_path 
    except Exception, e:  
        print e  
        return '' 

def mkdir(data):
	t = time.strptime(data['datetime'],'%Y-%m-%d')
	y,m,d = t[0:3]
	y = str(y)
	m = str(m).zfill(2)
	d = str(d).zfill(2)
	path = 'storage/'+y + '/' + m +'/' + y + m + d + data['audio_title']
	#print path
	if not os.path.exists(path):
		os.makedirs(path)
	return path
if __name__ == "__main__":
	fdata = open('data.json')
	data = json.load(fdata)
	path = mkdir(data)
	url = env('URL_DEDAO_SHARE_AUDIO')+data['audioid']
	f = urllib2.urlopen(url)
	html = f.read()
	f.close()
	print '抓取文章成功'
	html_path = path+'/'+data['audio_title']+'.html'
	fhtml = open(path+'/'+data['audio_title']+'.html',"w")
	fhtml.write(html)
	fhtml.close();
	fjson = open(path+'/'+data['audio_title']+'.json',"w")
	fjson.write(json.dumps(data))
	print chardet.detect(html)

	d = pq(filename=html_path)
	
	#下载mp3 start
	audio_url =  d('#audio_url').attr('data-src')
	print '音频地址:'+audio_url
	audio_url_local =''
	audio_url_local = download_source(audio_url, path,data['audio_title']+'.mp3')
	print '下载音频成功:'+audio_url_local
	#下载mp3 end
        
	#print p
	f = open(path+data['audio_title']+'.txt', "w") 
	#下载图片 start
	j = 0
	for img_url in [i.attr('data-url') for i in d('img').items()]:
		if j ==0:
			img_url_local = download_source(img_url, path,data['audio_title']+'.jpg')
			d("img[data-url='"+img_url+"']").attr('src',img_url_local);
			print '下载图片成功:'+img_url_local
			break;
	#下载图片 end 
	html = d('#article-main').html()
	print html
	content = dehtml(html)
	print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
	f.write(html)
	tdata = {
	'title':data['audio_title'],
	'audio_price':data['audio_price'],
	'brief':data['audio_summary'],
    'image_original':data['audio_icon'],
    'image_local':'',
    'url_original': env('URL_DEDAO_SHARE_AUDIO')+data['audioid'],
    'url_original_id':data['audioid'],
    'audio_original': audio_url,
    'duration':data['duration'],
    'datetime':data['datetime'],
    'image':'',
    'content':content,
    'audio_local':audio_url_local,
    'audio':env('URL_STATIC')+audio_url_local,
    'created_at':time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
	}
	createTingshu(tdata)
	print '创建听书成功'

	#创建听书内容 end 

	

